package com.ruoyi.drinkwater.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.drinkwater.domain.BusWaterDrinksource;
import com.ruoyi.drinkwater.domain.BusWaterDrinksourceBorder;
import com.ruoyi.drinkwater.mapper.BusWaterDrinksourceBorderMapper;
import com.ruoyi.drinkwater.mapper.BusWaterDrinksourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.drinkwater.mapper.BusWaterSegmentWaterIntakeMapper;
import com.ruoyi.drinkwater.domain.BusWaterSegmentWaterIntake;
import com.ruoyi.drinkwater.service.IBusWaterSegmentWaterIntakeService;

/**
 * 水源地取水口Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-26
 */
@Service
public class BusWaterSegmentWaterIntakeServiceImpl implements IBusWaterSegmentWaterIntakeService {
    @Autowired
    private BusWaterSegmentWaterIntakeMapper busWaterSegmentWaterIntakeMapper;
    @Autowired
    private BusWaterDrinksourceBorderMapper busWaterDrinksourceBorderMapper;

    @Autowired
    private BusWaterDrinksourceMapper busWaterDrinksourceMapper;

    /**
     * 查询水源地取水口
     *
     * @param id 水源地取水口主键
     * @return 水源地取水口
     */
    @Override
    public BusWaterSegmentWaterIntake selectBusWaterSegmentWaterIntakeById(String id) {
        BusWaterSegmentWaterIntake waterIntake= busWaterSegmentWaterIntakeMapper.selectBusWaterSegmentWaterIntakeById(id);
        //查询取水口下关联拐点信息列表
        BusWaterDrinksourceBorder border = new BusWaterDrinksourceBorder();
        border.setSegmentid(id);
        //一级保护区拐点
        List<Map> oneList = new ArrayList<Map>();
        //二级保护区拐点
        List<Map> twoList = new ArrayList<Map>();
        //准保护区拐点
        List<Map> zhunList = new ArrayList<Map>();
        List<BusWaterDrinksourceBorder> borderslist = busWaterDrinksourceBorderMapper.selectBusWaterDrinksourceBorderList(border);
        if (null != borderslist && borderslist.size() > 0) {
            //循环查询一级，二级，准保护区拐点，获取相应经纬度
            for (BusWaterDrinksourceBorder bor : borderslist) {
                Map map = new HashMap();
                map.put("pointnum", bor.getPointnum());
                map.put("longitude", bor.getLongtitude());
                map.put("latitude", bor.getLatitude());
                if ("1".equals(bor.getDrinksourcelevel())) {
                    oneList.add(map);
                } else if ("2".equals(bor.getDrinksourcelevel())) {
                    twoList.add(map);
                } else {
                    zhunList.add(map);
                }
            }
        }
        waterIntake.setOneProtectInfle(oneList);
        waterIntake.setTwoProtectInfle(twoList);
        waterIntake.setProtectInfle(zhunList);
        return waterIntake;
    }

    /**
     * 查询水源地取水口列表
     *
     * @param busWaterSegmentWaterIntake 水源地取水口
     * @return 水源地取水口
     */
    @Override
    public List<BusWaterSegmentWaterIntake> selectBusWaterSegmentWaterIntakeList(BusWaterSegmentWaterIntake busWaterSegmentWaterIntake) {
        List<BusWaterSegmentWaterIntake> list = busWaterSegmentWaterIntakeMapper.selectBusWaterSegmentWaterIntakeList(busWaterSegmentWaterIntake);

        for (BusWaterSegmentWaterIntake waterIntake : list) {
            //查询取水口下关联拐点信息列表
            BusWaterDrinksourceBorder border = new BusWaterDrinksourceBorder();
            border.setSegmentid(waterIntake.getId());
            //一级保护区拐点
            List<Map> oneList = new ArrayList<Map>();
            //二级保护区拐点
            List<Map> twoList = new ArrayList<Map>();
            //准保护区拐点
            List<Map> zhunList = new ArrayList<Map>();
            List<BusWaterDrinksourceBorder> borderslist = busWaterDrinksourceBorderMapper.selectBusWaterDrinksourceBorderList(border);
            if (null != list && list.size() > 0) {
                //循环查询一级，二级，准保护区拐点，获取相应经纬度
                for (BusWaterDrinksourceBorder bor : borderslist) {
                    Map map = new HashMap();
                    map.put("pointnum", bor.getPointnum());
                    map.put("longitude", bor.getLongtitude());
                    map.put("latitude", bor.getLatitude());
                    if ("1".equals(bor.getDrinksourcelevel())) {
                        oneList.add(map);
                    } else if ("2".equals(bor.getDrinksourcelevel())) {
                        twoList.add(map);
                    } else {
                        zhunList.add(map);
                    }
                }
            }
            waterIntake.setOneProtectInfle(oneList);
            waterIntake.setTwoProtectInfle(twoList);
            waterIntake.setProtectInfle(zhunList);
        }
        return list;
    }

    /**
     * 新增水源地取水口
     *
     * @param busWaterSegmentWaterIntake 水源地取水口
     * @return 结果
     */
    @Override
    public int insertBusWaterSegmentWaterIntake(BusWaterSegmentWaterIntake busWaterSegmentWaterIntake) {
        String drinkSourceID = busWaterSegmentWaterIntake.getDrinksourceid();
        String drinkSourceCodes=null;
        String citys=null;
        String cityCodes=null;
        String countys=null;
        String countyCodes=null;
        if(drinkSourceID!=null&&!"".equals(drinkSourceID)){
            BusWaterDrinksource wqBasDrinksource = busWaterDrinksourceMapper.selectBusWaterDrinksourceById(drinkSourceID);
            String drinkSourceCode = wqBasDrinksource.getDrinksourcecode();
            if(drinkSourceCode!=null&&!"".equals(drinkSourceCode)){
                drinkSourceCodes=drinkSourceCode;
            }
            String city = wqBasDrinksource.getCity();
            if(city!=null&&!"".equals(city)){
                citys=city;
            }
            String cityCode = wqBasDrinksource.getCitycode();
            if(cityCode!=null&&!"".equals(cityCode)){
                cityCodes=cityCode;
            }
            String county = wqBasDrinksource.getCounty();
            if(county!=null&&!"".equals(county)){
                countys=county;
            }
            String countyCode = wqBasDrinksource.getCountycode();
            if(countyCode!=null&&!"".equals(countyCode)){
                countyCodes=countyCode;
            }
        }
        busWaterSegmentWaterIntake.setSegmentdrinkcode(drinkSourceCodes);
        busWaterSegmentWaterIntake.setCity(citys);
        busWaterSegmentWaterIntake.setCitycode(cityCodes);
        busWaterSegmentWaterIntake.setCounty(countys);
        busWaterSegmentWaterIntake.setCountycode(countyCodes);
        busWaterSegmentWaterIntake.setCreateTime(DateUtils.getNowDate());
        busWaterSegmentWaterIntake.setId(UUID.randomUUID().toString());
//        WqBasDrinksource wqBasDrinksource = wqBasDrinksourceMapper.selectWqBasDrinksourceByID(busWaterSegmentWaterIntake.getDrinkSourceID());
//        if (null != wqBasDrinksource){
//            wqBasSegmentWaterIntake.setCounty(wqBasDrinksource.getCounty());
//            wqBasSegmentWaterIntake.setCountyCode(wqBasDrinksource.getCountyCode());
//            wqBasSegmentWaterIntake.setSegmentDrinkCode(wqBasDrinksource.getDrinkSourceCode());
//        }
        if (null != busWaterSegmentWaterIntake.getOneProtectInfle()){
            //一级水源地拐点信息保存
            List<Map> oneList =  busWaterSegmentWaterIntake.getOneProtectInfle();
            this.addBorder(busWaterSegmentWaterIntake,oneList,"1");
        }
        if (null != busWaterSegmentWaterIntake.getTwoProtectInfle()){
            //二级水源地保护区拐点信息保存
            List<Map> twoList =  busWaterSegmentWaterIntake.getTwoProtectInfle();
            this.addBorder(busWaterSegmentWaterIntake,twoList,"2");
        }
        if (null != busWaterSegmentWaterIntake.getProtectInfle()){
            //准保护区水源地拐点信息保存
            List<Map> zhunList =  busWaterSegmentWaterIntake.getProtectInfle();
            this.addBorder(busWaterSegmentWaterIntake,zhunList,"3");
        }
        busWaterSegmentWaterIntake.setIsdrink("1");
        busWaterSegmentWaterIntake.setLifecycle("1");
        busWaterSegmentWaterIntake.setCreatetime(DateUtils.dateFormat(new Date(),"yyyy-MM-dd HH:mm:ss"));
        return busWaterSegmentWaterIntakeMapper.insertBusWaterSegmentWaterIntake(busWaterSegmentWaterIntake);
    }

    /**
     * 修改水源地取水口
     *
     * @param busWaterSegmentWaterIntake 水源地取水口
     * @return 结果
     */
    @Override
    public int updateBusWaterSegmentWaterIntake(BusWaterSegmentWaterIntake busWaterSegmentWaterIntake) {
        //先删除原始水源地拐点信息
        busWaterDrinksourceBorderMapper.deleteBusWaterDrinksourceBorderById(busWaterSegmentWaterIntake.getId());
        //保存修改后的水源地拐点信息
        if (null != busWaterSegmentWaterIntake.getOneProtectInfle()){
            //一级水源地拐点信息保存
            List<Map> oneList =  busWaterSegmentWaterIntake.getOneProtectInfle();
            this.addBorder(busWaterSegmentWaterIntake,oneList,"1");
        }
        if (null != busWaterSegmentWaterIntake.getTwoProtectInfle()){
            //二级水源地保护区拐点信息保存
            List<Map> twoList =  busWaterSegmentWaterIntake.getTwoProtectInfle();
            this.addBorder(busWaterSegmentWaterIntake,twoList,"2");
        }
        if (null != busWaterSegmentWaterIntake.getProtectInfle()){
            //准保护区水源地拐点信息保存
            List<Map> zhunList =  busWaterSegmentWaterIntake.getProtectInfle();
            this.addBorder(busWaterSegmentWaterIntake,zhunList,"3");
        }
        String drinkSourceID = busWaterSegmentWaterIntake.getDrinksourceid();
        String drinkSourceCodes=null;
        String citys=null;
        String cityCodes=null;
        String countys=null;
        String countyCodes=null;
        if(drinkSourceID!=null&&!"".equals(drinkSourceID)){
            BusWaterDrinksource wqBasDrinksource = busWaterDrinksourceMapper.selectBusWaterDrinksourceById(drinkSourceID);
            String drinkSourceCode = wqBasDrinksource.getDrinksourcecode();
            if(drinkSourceCode!=null&&!"".equals(drinkSourceCode)){
                drinkSourceCodes=drinkSourceCode;
            }
            String city = wqBasDrinksource.getCity();
            if(city!=null&&!"".equals(city)){
                citys=city;
            }
            String cityCode = wqBasDrinksource.getCitycode();
            if(cityCode!=null&&!"".equals(cityCode)){
                cityCodes=cityCode;
            }
            String county = wqBasDrinksource.getCounty();
            if(county!=null&&!"".equals(county)){
                countys=county;
            }
            String countyCode = wqBasDrinksource.getCountycode();
            if(countyCode!=null&&!"".equals(countyCode)){
                countyCodes=countyCode;
            }
        }
        busWaterSegmentWaterIntake.setSegmentdrinkcode(drinkSourceCodes);
        busWaterSegmentWaterIntake.setCity(citys);
        busWaterSegmentWaterIntake.setCitycode(cityCodes);
        busWaterSegmentWaterIntake.setCounty(countys);
        busWaterSegmentWaterIntake.setCountycode(countyCodes);
        return busWaterSegmentWaterIntakeMapper.updateBusWaterSegmentWaterIntake(busWaterSegmentWaterIntake);
    }

    /**
     * 批量删除水源地取水口
     *
     * @param ids 需要删除的水源地取水口主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterSegmentWaterIntakeByIds(String[] ids) {
        busWaterDrinksourceBorderMapper.deleteBusWaterDrinksourceBorderByIds(ids);
        return busWaterSegmentWaterIntakeMapper.deleteBusWaterSegmentWaterIntakeByIds(ids);
    }

    /**
     * 删除水源地取水口信息
     *
     * @param id 水源地取水口主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterSegmentWaterIntakeById(String id) {
        busWaterDrinksourceBorderMapper.deleteBusWaterDrinksourceBorderById(id);
        return busWaterSegmentWaterIntakeMapper.deleteBusWaterSegmentWaterIntakeById(id);
    }

    private void addBorder(BusWaterSegmentWaterIntake busWaterSegmentWaterIntake, List<Map> mapList, String level){
        BusWaterDrinksourceBorder border = new BusWaterDrinksourceBorder();
        for (Map map : mapList) {
            border.setId(UUID.randomUUID().toString());
            border.setDrinksourceid(busWaterSegmentWaterIntake.getDrinksourceid());
            border.setSegmentid(busWaterSegmentWaterIntake.getId());
            border.setLatitude(map.get("latitude").toString());
            border.setLongtitude(map.get("longitude").toString());
            border.setPointnum(map.get("pointnum").toString());
            border.setDrinksourcelevel(level);
            busWaterDrinksourceBorderMapper.insertBusWaterDrinksourceBorder(border);
        }
    }
}
